package com.example.mydemo.controller.hc;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mydemo.config.ReVo;
import com.example.mydemo.entity.domain.ResponseDTO;
import com.example.mydemo.entity.SysParam;
import com.example.mydemo.log.LogRequest;
import com.example.mydemo.mapper.SysParamMapper;
import com.example.mydemo.entity.Users;
import com.example.mydemo.model.tableVO;
import com.example.mydemo.service.SysParamService;
import com.example.mydemo.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

    @Controller
    @RequestMapping()
    public class SysParamController {
        private final SysParamService sysParamService;
        @Autowired
        private SysParamMapper sysParamMapper;
        @Autowired
        private RedisUtil redisUtil;
        public SysParamController(SysParamService sysParamService) {
            this.sysParamService = sysParamService;
        }

//        @GetMapping("/value/{code}")
//
//        @ResponseBody
//        public ResponseEntity<String> getValueByCode(@PathVariable String code) {
//            String value = sysParamService.getParmValueByCode(code);
//            return ResponseEntity.ok().body(value);
//        }
//
//        @PutMapping("/value/{code}")
//
//        @ResponseBody
//        public ResponseEntity<Boolean> updateValue(
//                @PathVariable String code,
//                @RequestParam String value
//        ) {
//            boolean success = sysParamService.updateParmValue(code, value);
//            return ResponseEntity.ok().body(success);
//        }
    @GetMapping("/getParamById")
    @ResponseBody
    public ResponseDTO<SysParam> getList(@RequestParam Long id) {
            SysParam sysParam = sysParamMapper.selectById(id);
            if (sysParam.getIsDelete()==0L){
                return new ResponseDTO<>(200, "获取信息成功！", sysParam);
            }else {
            return new ResponseDTO<>(200, "获取信息失败！", sysParam);
            }
    }

    @GetMapping("/getsysparmpage")
    @ResponseBody
    public tableVO getsysparmpage(@RequestParam Long page, @RequestParam Long limit,@RequestParam(defaultValue = "") String searchParams) {
        Page<SysParam> codepage = new Page<>(page, limit); // 创建分页对象，当前页为1，每页显示5条记录
        IPage<SysParam> sysParamPagePage = sysParamMapper.selectPage(codepage, new UpdateWrapper<SysParam>().eq("is_delete", 0L));

        tableVO a =new tableVO();
        a.setCode(0L);
        a.setMsg("");
        if (searchParams ==""){
            a.setData(sysParamPagePage.getRecords());
        }else {
            a.setData(sysParamService.selectByName(searchParams));
        }
        a.setCount((long) sysParamPagePage.getTotal());
//        ResponseEntity<List<SysParm>> a =ResponseEntity.ok(b);
        return a;
    }

        @PostMapping("/paramAdd")
        @ResponseBody
        public ResponseDTO<SysParam> paramAdd(@RequestParam String paramCode,@RequestParam String paramName,@RequestParam String paramValue) {
            SysParam sysParam = new SysParam();
            sysParam.setParamValue(paramValue);
            sysParam.setParamCode(paramCode);
            sysParam.setParamName(paramName);
            if (sysParamService.save(sysParam)){
                return new ResponseDTO<>(200, "添加信息成功！", sysParam);
            }
            return new ResponseDTO<>(404, "添加信息失败！", sysParam);
        }
        @PostMapping("/paramDelete")
        @ResponseBody
        public ResponseDTO<SysParam> paramDelete(@RequestParam Long id) {
            SysParam sysParam = new SysParam();
            sysParam.setIsDelete(1L);
            int updated = sysParamMapper.update(
                    sysParam, // 显式传递实体对象
                    new UpdateWrapper<SysParam>().eq("id", id)
            );
            return new ResponseDTO<SysParam>(200, "添加信息成功！", sysParam);
        }
    }
